package com.shanshi.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shanshi.activity.R;
import com.shanshi.activity.response.ResStoreInfo;

import java.util.List;

/**
 * Created by scx on 2017/3/16.
 */

public class StoreInfoAdapter extends BaseAdapter{
    private Context context;
    private List<ResStoreInfo.Data> list;
    private LayoutInflater from;
    private ViewHolder holder=null;
    private int nowStoreId; //当前门店id


    /*********存放店铺列表每一个item的组件类**********/
    private static class ViewHolder{
        ImageView allstoreinfo_coverimg;   //图片
        TextView allstoreinfo_storeName;  //店铺名称
        TextView allstoreinfo_storeAddr;   //店铺地址
        TextView allstoreinfo_storeTel;   //店铺电话
        TextView allstoreinfo_distence;  //距离
        TextView allstoreinfo_now;  //当前门店
    }

    public StoreInfoAdapter(Context context, List<ResStoreInfo.Data> list,int nowStoreId) {
        from = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        this.nowStoreId = nowStoreId;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView=from.inflate( R.layout.allstoreinfo, null);
            holder=new ViewHolder();
            holder.allstoreinfo_coverimg=(ImageView) convertView.findViewById(R.id.allstoreinfo_coverimg);
            holder.allstoreinfo_storeName=(TextView) convertView.findViewById(R.id.allstoreinfo_storeName);
            holder.allstoreinfo_storeAddr=(TextView) convertView.findViewById(R.id.allstoreinfo_storeAddr);
            holder.allstoreinfo_storeTel=(TextView) convertView.findViewById(R.id.allstoreinfo_storeTel);
            holder.allstoreinfo_distence=(TextView) convertView.findViewById(R.id.allstoreinfo_distence);
            holder.allstoreinfo_now=(TextView) convertView.findViewById(R.id.allstoreinfo_now);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }

        Glide.with(context).load(list.get(position).getCoverImg()).centerCrop().into(holder.allstoreinfo_coverimg);
        holder.allstoreinfo_storeName.setText(list.get(position).getStoreName());
        holder.allstoreinfo_storeAddr.setText(list.get(position).getStoreAddress());
        holder.allstoreinfo_storeTel.setText("电话："+list.get(position).getTelPhone());
        holder.allstoreinfo_distence.setText(list.get(position).getDistance()+"KM");
        holder.allstoreinfo_now.setVisibility(View.GONE);
        //为当前门店设置标识
        if(list.get(position).getStoreId()==nowStoreId){
            holder.allstoreinfo_now.setVisibility(View.VISIBLE);
        }
//      myViewHolder.tv.setText((Integer) list.get(position).get("storeName"));
//      myViewHolder.tv.setText((Integer) list.get(position).get("storeAdd"));
        return convertView;
    }
}
